package dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import bean.StudentBean;
import bean.UniversityBean;

import db.DbRow;
import db.DbSelect;
import db.DbTableStudent;

public class StudentDAO extends UniversityDAO {

	private DbTableStudent st;
	static private ArrayList <StudentBean> ArrStydents = null;

	public StudentDAO() throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		st = new DbTableStudent();
		if (getArrStydents() == null)
			setArrStydents(new ArrayList<StudentBean>());
	}

	public ArrayList<StudentBean> InitStudents() throws SQLException {
		DbSelect select = (DbSelect) st.select().from(st._getName());
		ArrayList<DbRow> ar = (ArrayList<DbRow>) st.fetchAll(select);

		for (int i = 0; i < ar.size(); i++) {
			DbRow row = ar.get(i);
			StudentBean st = new StudentBean((String) row.getColomn("nume"),
					(String) row.getColomn("prenume"),
					(String) row.getColomn("telefon"),
					(Integer) row.getColomn("id_group"),
					(Integer) row.getColomn("id"));
			getArrStydents().add(st);
		}

		return getArrStydents();
	}

	public int Insert(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		
		int idReturned=0;
		
		StudentBean st = (StudentBean) object;
		DbRow row = new DbRow(4);

		row.setRow(st.getNume(), 0, "nume");
		row.setRow(st.getPrenume(), 1, "prenume ");
		row.setRow(st.getTel(), 2, "telefon");
		row.setRow(st.getIdGr(), 3, "id_group");

		DbTableStudent TableStudent = new DbTableStudent();
		
		idReturned=TableStudent.insert(row);
		
		st.setId(idReturned);
		
		getArrStydents().add(st);
		return idReturned;
	}

	public ArrayList<StudentBean> getArrayListStudents() {
		return getArrStydents();
	}

	@Override
	public
	void Delet(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		StudentBean stBean=(StudentBean) object;
		
		DbTableStudent tableStudent =new DbTableStudent();
		tableStudent.delete("id="+stBean.getId());
		
		getArrStydents().remove(stBean);
	}

	@Override
	public void update(UniversityBean object) throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
		StudentBean temp;
		StudentBean stBean=(StudentBean) object;
		DbTableStudent tableStudent =new DbTableStudent();
		
		DbRow row = new DbRow(4);
		
		row.setRow(stBean.getNume(), 0, "nume");
		row.setRow(stBean.getPrenume(), 1, "prenume ");
		row.setRow(stBean.getTel(), 2, "telefon");
		row.setRow(stBean.getIdGr(), 3, "id_group");

		tableStudent.update(row, "id="+stBean.getId());
		
		for(int i=0;i<getArrStydents().size();i++){
			temp=(StudentBean) getArrStydents().get(i);
			if(temp.getId()==stBean.getId()){
				getArrStydents().remove(i);
				getArrStydents().add(stBean);
			}
		}		
	}

	@Override
	public Vector<String> getUniversityDAOById(int id) {
		StudentBean temp;
		Vector<String> studentVector = new Vector<String>();
		
		System.out.println(getArrStydents().size()+" astai lungimea");
		
		for(int i=0;i<getArrStydents().size();i++){
			temp=(StudentBean) getArrStydents().get(i);
			System.out.println(temp.getId()+" and "+id);
			if(temp.getId()==id){
				studentVector.add(temp.getNume());
				studentVector.add(temp.getPrenume());
				studentVector.add(temp.getTel());
				return studentVector;
			}
		}		
		return null;
	}

	public static ArrayList<StudentBean> getArrStydents() {
		return  ArrStydents;
	}

	public static void setArrStydents(ArrayList<StudentBean> arrStydents) {
		ArrStydents = arrStydents;
	}
}
